package com.psc.view.utils;

import android.app.Activity;
import android.content.Context;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import androidx.core.content.ContextCompat;

import com.psc.R;
import com.psc.view.customized.SupportPopupWindow;
import com.psc.view.ui.a.adapter.TabGridAdapter;

import java.util.List;

/**
 * Created : Yx
 * Time : 2018/11/7 17:12
 * Describe :选择弹窗
 */
public class PopupWindowUtil {

    public static PopupWindow popupWindow = null;
    public static SupportPopupWindow popupWindow2 = null;

    public interface onYxCallback {
        void onSuccess(String result);

        void onFail();
    }

    /**
     * Tab
     *
     * @param mContexts
     * @param view      在该view的下面
     */
    public static void showTab(final Context mContexts, View view, final List<String> goodsTypes, int positions, final onYxCallback callback) {
        View parent;
        if (view == null) {
            //view为空时设定一个顶级视图,它包含整个屏幕，包括标题栏
            parent = ((Activity) mContexts).getWindow().getDecorView();
        } else {
            parent = view;
        }

        // 一个自定义的布局，作为显示的内容
        View contentView = LayoutInflater.from(mContexts).inflate(R.layout.pop_tab, null);
        popupWindow2 = new SupportPopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        popupWindow2.setTouchable(true);
        popupWindow2.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
        popupWindow2.setBackgroundDrawable(ContextCompat.getDrawable(mContexts, R.drawable.bg_transparent_mengban));
        popupWindow2.showAsDropDown(parent);
        popupWindow2.setOutsideTouchable(false);//以外的区域是否可点击
        //当popWindow消失时的监听
        //popupWindow.setOnDismissListener();
        contentView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {// 按返回键对话框消失，透明度恢复
                        if (callback != null) {
                            callback.onFail();
                        }
                        hindPopupWindow2();
                    }
                }
                return false;
            }
        });
        contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callback != null) {
                    callback.onFail();
                }
                hindPopupWindow2();
            }
        });
        // --------------------------------------控件操作↓--------------------
        GridView gridView = contentView.findViewById(R.id.tab_gridview);
        final TabGridAdapter adapter = new TabGridAdapter(mContexts);
        gridView.setAdapter(adapter);
        adapter.setData(goodsTypes, positions);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //adapter.onRefresh(position);
                if (callback != null) {
                    callback.onSuccess(position + "");
                }
                hindPopupWindow2();
            }
        });
        ImageView tab_shangla = contentView.findViewById(R.id.tab_shangla);
        tab_shangla.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callback != null) {
                    callback.onFail();
                }
                hindPopupWindow2();
            }
        });


    }

    public static void hindPopupWindow2() {
        if (popupWindow2 != null) {
            popupWindow2.dismiss();
            popupWindow2 = null;
        }
    }

}
